package com.xuecheng.content.controller;

import com.xuecheng.api.contant.CourseBaseApi;
import com.xuecheng.api.contant.model.dto.CourseBaseDTO;
import com.xuecheng.api.contant.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.contant.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {


    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 提交课程
     * @param courseBaseId 课程id
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        // 1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        // 2、调用service层方法,无返回值
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }


    /**
     * 删除课程基本信息
     * @param courseBaseId 课程基本信息id
     */
    @DeleteMapping("course/{courseBaseId}")
    public void removeCourseBase(@PathVariable Long courseBaseId) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        // 调用方法
        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }


    /**
     * 根据Id获取课程基本信息
     * @param courseBaseId 课程基本信息id
     * @return 返回CourseBaseDTO
     */
    @GetMapping(value = "course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = courseBaseService.getByCourseId(courseBaseId, companyId);
        return courseBaseDTO;
    }


    /**
     * 更新课程基本信息
     * @param courseBaseVO 课程基本信息VO
     * @return 返回CourseBaseDTO
     */
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVO);
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO baseDTO = courseBaseService.modifyCourseBase(courseBaseDTO);
        return baseDTO;
    }


    /**
     * 添加课程基本信息
     * @param vo CourseBaseVO前端传过来的VO实体类数据
     * @return 返回DTO数据给前端
     */
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(vo);
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO baseDTO = courseBaseService.createCourseBase(courseBaseDTO);
        return baseDTO;
    }


    /**
     * 分页条件查询课程基本信息数据,并实现机构隔离的依据数据获得
     * @param params PageRequestParams 分页封装数据
     * @param model QueryCourseBaseModel 条件查询封装数据
     * @return PageVO 分页结果封装数据
     */
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseBaseModel model) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        PageVO<CourseBaseDTO> pageVO = courseBaseService.queryCourseBaseList(params, model,companyId);
        return pageVO;
    }


    /**
     * 查询课程基本信息
     * @return List<CourseBase> 课程基本信息的集合
     */
    @GetMapping("/cource/all")
    public List<CourseBase> queryAll() {
        List<CourseBase> courseBaseList = courseBaseService.queryAll();
        return courseBaseList;
    }
}
